from datetime import datetime
from typing import Optional, Literal

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from pydantic_validation_decorator import NotBlank

from module_admin.annotation.pydantic_annotation import as_query


class AccountModel(BaseModel):
    """
    股票账号表对应pydantic模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)

    id: Optional[int] = Field(default=None, description='主键ID')
    account_no: Optional[str] = Field(default=None, description='账号')
    account_tag: Optional[str] = Field(default=None, description='账号标签')
    broker: Optional[str] = Field(default=None, description='券商')
    account_name: Optional[str] = Field(default=None, description='账号名')
    miniqmt: Optional[str] = Field(default=None, description='MiniQMT')
    del_flag: Optional[Literal['0', '2']] = Field(default=None, description='删除标志（0代表存在 2代表删除）')
    create_by: Optional[str] = Field(default=None, description='创建者')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')
    update_by: Optional[str] = Field(default=None, description='更新者')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')
    remark: Optional[str] = Field(default=None, description='备注')

    @NotBlank(field_name='account_no', message='账号不能为空')
    def get_account(self):
        return self.account_no

    @NotBlank(field_name='broker', message='券商不能为空')
    def get_broker(self):
        return self.broker

    @NotBlank(field_name='account_name', message='账号名不能为空')
    def get_account_name(self):
        return self.account_name

    @NotBlank(field_name='account_tag', message='账号标签不能为空')
    def get_account_tag(self):
        return self.account_tag

    @NotBlank(field_name='miniqmt', message='MiniQMT不能为空')
    def get_miniqmt(self):
        return self.miniqmt

    def get_del_flag(self):
        return self.del_flag

    def validate_fields(self):
        self.get_account()
        self.get_broker()
        self.get_account_name()
        self.get_account_tag()
        self.get_miniqmt()
        self.get_del_flag()


class AccountQueryModel(AccountModel):
    """
    股票账号不分页查询模型
    """
    pass


@as_query
class AccountPageQueryModel(AccountQueryModel):
    """
    股票账号分页查询模型
    """

    page_num: int = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页记录数')


class DeleteAccountModel(BaseModel):
    """
    删除股票账号模型
    """

    model_config = ConfigDict(alias_generator=to_camel)

    ids: str = Field(description='需要删除的主键ID')
    update_by: Optional[str] = Field(default=None, description='更新者')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')
